package com.github.eclipseace.mybatis.spring;

import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.StringUtils;

/**
 * @author eclipseAce
 */
public class EntityStoreRegistrar implements ImportBeanDefinitionRegistrar {
	@Override
	public void registerBeanDefinitions(AnnotationMetadata anMetadata, BeanDefinitionRegistry registry) {
		AnnotationAttributes anAttrs = AnnotationAttributes.fromMap(
				anMetadata.getAnnotationAttributes(EnableEntityStore.class.getName()));
		String beanName = anAttrs.getString("beanName");
		if (beanName.isEmpty()) {
			beanName = StringUtils.uncapitalize(EntityStore.class.getSimpleName());
		}
		String[] basePackages = anAttrs.getStringArray("basePackages");

		GenericBeanDefinition definition = new GenericBeanDefinition();
		definition.setBeanClass(ClassPathEntityStore.class);
		definition.getPropertyValues().addPropertyValue("basePackages", basePackages);
		definition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);
		registry.registerBeanDefinition(beanName, definition);
	}
}
